﻿import QtQuick 2.11
//import QtQuick.Controls 1.4
import QtGraphicalEffects 1.0

import com.leamus.gamedata 1.0

import "../"
import "../_Global"
import "../GameJS.js" as GameJS

GameItem {
    //property int type   //
    property bool bRectangularGlow: false   //是否显示点击后发光边框
    property bool bTemp: false              //是临时牌,则bEnable和bClicked和nFlags无效(bClicked自动设置)
    property bool bZhengMian : true         //: (nFlags & PokeClass.flag_ZhengMian)
    //property int iZBackup                   //z序备份,目前没用
    property bool bBiggerEnabled: false     //点击后是否放大
    //property bool bBigger: false            //是否鼠标进入后放大

    property alias pokeSource : image.source
    property alias pokeBackSource : imageBack.source

    property alias sourceSize: image.sourceSize
    property alias mouseArea: mouseArea

    property alias rectGlow: rectGlow       //边框效果


    signal s_debugMessage(string str)



    function aniMoveTo(dx,dy) {
        _aniMoveTo(dx,dy,1000);
    }



    id: root
    //enabled: false
    visible: (opacity == 0 ? false : true)
    objectName: "pokeObject"
    transformOrigin: Item.Center    //!!!坐标为中心点
    //width: image.width
    //height: image.height

    bEnable: (nFlags & PokeClass.Flag_Enable)           //是否可点
    bClicked: (nFlags & PokeClass.Flag_Clicked)          //点击状态

    //scale: bBigger ? 1.03 : 1

//这里是边框效果
    RectangularGlow {
        id: rectGlow
        visible: bRectangularGlow && root.bEnable && (root.bClicked)// || mouseArea.containsMouse)
        anchors.fill: rect
        glowRadius: 10  //边框宽度
        spread: 0.2    //虚化程度(0~1)
        color: root.bClicked ? "lightgreen" : "blue"  //颜色
        //color: root.bClicked ? "lightgreen" : "blue"  //颜色

        //z: -1
        cornerRadius: rect.radius + glowRadius
    }

//牌的矩形
    Rectangle {
        id: rect
        width: root.width
        height: root.height
        color: "#00000000"

        //property alias buttonText: textItem.text	//


        //图片
        Image {
            id: image
            width: parent.width
            height: parent.height
            antialiasing: true
            //source: "Pai101.jpg"
            //anchors.fill: parent

        }

        Image {
            id: imageBack
            width: parent.width
            height: parent.height
            visible: false
            antialiasing: true
            //source: "Pai101.jpg"
            //anchors.fill: parent

        }

        //阴影
        DropShadow {
            visible: false
            anchors.fill: image; source: image
            horizontalOffset: 4; verticalOffset: 4
            radius: 12.0; samples: 16; color: "#80000000";
        }


        //鼠标事件
        MouseArea {
            id: mouseArea
            //enabled: root.bEnable
            hoverEnabled: true
            anchors.fill: image
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            //propagateComposedEvents: true //当为true时，可以传递鼠标事件。默认值为false

            Timer { //鼠标放在上面的时间
                id: timer
                interval: 1000
                repeat: false
                running: false
                triggeredOnStart: false
                onTriggered: {
                    s_holdTriggered(vUserData)
                }
            }
            onEntered: {
                if(bEnable)timer.start()
                if(bEnable && bBiggerEnabled) {
                    root.scale = 1.03;
                    root.z += 1;
                }
                //console.debug("scale:",bEnable , bBiggerEnabled)
            }
            onExited: {
                if(root.scale != 1) {
                    _private.mouseExited();
                }
            }

            onClicked: {
                //console.log(mouse.x);
                //console.log(width);
                //console.debug("poke data:",vUserData,",",nFlags,",",root.z,root.visible,root.opacity);
                if ( mouse.button == Qt.LeftButton ) {
                    if(bEnable) {
                        //nFlags ^= PokeClass.flag_Clicked
                        root.s_clicked(bClicked, vUserData);
                        //console.debug(bClicked)
                        if(bTemp) {
                            bClicked = !bClicked
                            //bClicked = true;
                        }
                        //root.scale = 1;
                        //console.debug("qml中点击是:",vUserData)
                    }
                    else if(root.opacity > 0) {
                        mouse.accepted = true
                    }
                    else
                        mouse.accepted = false
                }
                else {
                    //console.debug("poke data:",vUserData,",",nFlags,",",root.z,root.visible,root.opacity)
                    //s_debugMessage("poke data:" + vUserData + "," + nFlags + "," + root.z,root.visible,root.opacity)
                }

                //console.debug("onClicked")
            }
            ////onEntered: if(bEnable){root.scale = 1.03; root.z += 1;}
            ////onExited: if(bEnable){root.scale = 1; root.z -= 1;}
            /*onDoubleClicked: {
                console.debug("poke data:",vUserData,",",nFlags,",",root.z,root.visible,root.opacity);
                s_debugMessage("poke data:" + vUserData + "," + nFlags + "," + root.z,root.visible,root.opacity);
                mouse.accepted = false;
            }*/
            //onPressAndHold: console.debug("onPressAndHold")
            //onPressed: console.debug("onPressed")
            //onReleased: console.debug("onReleased")
        }


/*
        Glow {
            anchors.fill: image; source: image
            radius: 16; samples: 24
            color: "red"; spread: 0.01
            fast: true;
        }
        */


    }

    //颜色遮罩(不可用时黑色,source必须和本对象同一层次!!!)
    ColorOverlay {
        id: colorOverlay
        visible: !root.bEnable
        anchors.fill: rect
        source: rect
        color: "#80000000"
    }



    onBEnableChanged: {
        if(bEnable == false && root.scale != 1) {
            //console.debug("mouseExited:",vUserData)
            _private.mouseExited();
        }
    }
    onBZhengMianChanged: {
        _private.changeSource();
    }

    QtObject {
        id: _private

        function changeSource() {
            if(bZhengMian) {
                image.visible = true;
                imageBack.visible = false;
            }
            else {
                image.visible = false;
                imageBack.visible = true;
            }
        }
        function mouseExited() {
            timer.stop()
            root.scale = 1;
            root.z -= 1;
            //console.debug("scale:",bEnable , bBiggerEnabled)
        }
    }

    Component.onCompleted: {
    }


    //onPokeSourceChanged: _private.changeSource()
    //onPokeBackSourceChanged: _private.changeSource()
}

